package edu.nuc.event.controller;


import edu.nuc.event.pojo.Notices;
import edu.nuc.event.pojo.User;
import edu.nuc.event.service.ComService;
import edu.nuc.event.service.NoticesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class NoticesController {

    @Autowired
    private NoticesService noticesService;

    @Autowired
    private ComService comService;

    @GetMapping("/toselect_notice")
    public String toselectnotice(Model model, HttpSession session) {

        List<Notices> noticesList = noticesService.seletAllNotices();

        model.addAttribute("notices", noticesList);

        int account = noticesService.selectnoticesaccount();
        model.addAttribute("account", account);

        return "toselect_notice";
    }

    @GetMapping("/notice-operation-admin")
    public String noticeoperationadmin(Model model, HttpSession session) {
        User loginuser = (User) session.getAttribute("loginuser");
        if (loginuser.getCpid() == 0) {
            return "redirect:/noplay";
        } else {

            int cpid = loginuser.getCpid();

            List<Notices> noticesList = noticesService.selectNoticesByCid(cpid);

            model.addAttribute("notices", noticesList);

            int account = noticesService.NoticesByCidAccount(cpid);
            model.addAttribute("account", account);

            return "notice-operation-admin";
        }
    }

    @GetMapping("/notice-operation-judge")
    public String noticeoperationjudge(Model model, HttpSession session) {
        User loginuser = (User) session.getAttribute("loginuser");
        if (loginuser.getCpid() == 0) {
            return "redirect:/noplay";
        } else {

            int cpid = loginuser.getCpid();

            List<Notices> noticesList = noticesService.selectNoticesByCid(cpid);

            model.addAttribute("notices", noticesList);

            int account = noticesService.NoticesByCidAccount(cpid);
            model.addAttribute("account", account);

            return "notice-operation-judge";
        }
    }

    @GetMapping("/toadd-notice")
    public String toaddnotice(Model model, HttpSession session) {
        User loginuser = (User) session.getAttribute("loginuser");
        model.addAttribute("user", loginuser);
        return "toadd-notice";
    }

    @PostMapping("/doadd-notice")
    public String doaddnotice(Notices notices) {

        int i = noticesService.addNotice(notices);

        return "redirect:/notice-operation-admin";
    }

    @GetMapping("/notice-operation-admin0")
    public String noticeoperationadmin0(Model model) {
        List<Notices> notices = noticesService.seletAllNoticesno();

        model.addAttribute("notices", notices);

        int account = noticesService.selectnoticesaccountno();

        model.addAttribute("account", account);

        return "notice-operation-admin0";
    }

    @GetMapping("/toupdate-notice")
    public String toupdatenotice(@RequestParam("id") int id, Model model) {
        Notices notices = noticesService.selectNoticeID(id);

        model.addAttribute("notices", notices);

        return "toupdate-notice";
    }

    @PostMapping("/doupdate-notice")
    public String doupdatenotice(Notices notices) {

//        System.out.println("======================");
//        System.out.println(notices);

        int i = noticesService.updatenaticetype(notices.getId(), notices.getNotype());

        return "redirect:/notice-operation-admin";
    }


}
